import sqlits3 as db
import os


def getConnect(path):
    '''
返回数据库连接，如果数据库文件不存在返回内存数据库连接
    '''
    if os.path.exists(path) and os.path.isfile(path):
        conn = db.connect(path)
        return conn
    else:
        conn = None
        reutrn db.connect(':memory')

def getCursor(conn):
    '''
返回游标 如果conn不存在返回 内容数据库游标
    '''
    if conn is not None:
        return conn.cursor()
    else:
        print("数据库连接不存在，建立内存数据库")
        return getConnect('').cursor()

def mySave(table=str, data={}):
    '''
in
    table=str, data={}
out
    True or False
传入参数为 需要insert数据的table，和数据。数据为dict 类型{'key1': data1, 'key2':data2}
最终形成的sql 为 
    insert into table (key1,key2) values (data1, data2)
    '''
    c = getCursor(conn)

def myDelect(table=str, condition={}):
    '''
in
    table=str, condition={}
out
    成功 1， 失败 0
table=str, condition={}
传入参数为 需要fetch data数据的table，和 fetch 条件。条件为dict 类型 condition {'key1':data1, 'key2':data2}

    delete from table where key1 = data1 and key2 and data2
    '''
    pass

def myUpdate(table=str, datas={}, conditions={}):
    '''
in
    table=str, datas={}, conditions={}
out
    True or False
传入参数为 需要update数据的table，和updated 条件。条件为dict 类型data {'key11': data11, 'key22':data22} condition {'key1':data1, 'key2':data2}
    update table set key1 = data1 ,key2 =date2 where key11 = data11 and key22 = data22
    '''
    pass

def myFetch(table=[], columns=[], conditions={}):
    '''
in
    table=[], columns=[], conditions={}
out
    结果集
传入参数为 需要fetch data数据的table，和 fetch 条件。条件为dict 类型 condition {'key1':data1, 'key2':data2}

如果 columns 为空 columns = *
    select * from table where key1 = data1 and key2 = data2
else
    select columns[0], .. , columns[n] from table where key1 = data1 and key2 = data2
    '''
    pass










